package academic.ui.client.presenter;



import java.util.ArrayList;

import academic.ui.client.AppServiceAsync;
import academic.ui.client.event.LoggedInEvent;
import academic.ui.client.event.LoggedInEventHandler;
import academic.ui.client.event.NewGameEvent;
import academic.ui.client.event.NewGameEventHandler;
import academic.ui.client.place.Tokens;

import com.allen_sauer.gwt.log.client.Log;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class PrimaryPresenter extends 
Presenter<PrimaryPresenter.MyView,PrimaryPresenter.MyProxy>{
	
	@ContentSlot
	public static final Type<RevealContentHandler<?>> slot = new Type<RevealContentHandler<?>>();
	private final PlaceManager placeManager;
	
	private final AppServiceAsync service;
	
	
	@Inject
	public PrimaryPresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager,AppServiceAsync service) {
		super(eventBus, view, proxy);
		this.placeManager=placeManager;
		this.service=service;
	}

	@ProxyStandard
	public interface MyProxy extends Proxy<PrimaryPresenter>  {}

	public interface MyView extends View {

		void setup();

		ArrayList<HasClickHandlers> getProductLinks();

		void unBindView();

		void removeEmailDiv();

		HasText emailInput();

		HasClickHandlers submitEmailButton();

	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("primaryPresenter onbind");
		registerHandler(getEventBus().addHandler(LoggedInEvent.getType(), new LoggedInEventHandler() {

			@Override
			public void onLoggedInEvent(LoggedInEvent event) {
				getView().removeEmailDiv();
			}
			
		}));
		
		registerHandler(getEventBus().addHandler(NewGameEvent.getType(), new NewGameEventHandler() {

			@Override
			public void onNewGameEvent(NewGameEvent event) {
				getView().removeEmailDiv();
			}
			
		}));
		
		registerHandler(getView().submitEmailButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				service.submitEmailToList(getView().emailInput().getText(),new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Boolean result) {
						if(result)
							placeManager.revealPlace(new PlaceRequest(Tokens.deals));
					}
					
				});
			}
			
		}));
	}
	
	@Override
	public void onReset() {
		super.onReset();
		Log.debug("PrimaryPresenter onReset");
		service.isLoggedIn(new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Boolean result) {
				if(!result && placeManager.getCurrentPlaceRequest().getNameToken().equals(Tokens.primary))
				{
					getView().setup();
				}
			}
			
		}); 
	}
	
	@Override
	public void onReveal() {
		super.onReveal();
		Log.debug("PrimaryPresenter onReveal");	
		getView().setup();	
	}
	
	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, AppPresenter.slot, this);
		Log.debug("PrimaryPresenter revealInParent");
	}

	
	@Override
	public void onHide() {
		super.onHide();
		Log.debug("PrimaryPresenter onHide");
	}
	
	
}
